import os
import csv
import time
import requests
from Scan3 import search_files_multithreaded

path = '/Users/Sheldon/Workspace/Weibo/WeiboMain/'
problemAB = {}
scanTimes = 0
fileName = "allonlne.csv"
token = 'nieyu2'
url = 'http://abtest.client.weibo.cn/downloadAllOnlineData?token=' + token
blackPathList = ['WBPlayerInternalExt']  # 不用扫描的黑名单


def loadAB():
    leng = 1
    print('开始拉取全量开关...')
    while (leng == 1):
        response = requests.get(url)
        leng = len(list(response.iter_content(1024)))  # 下载区块数
        if (leng == 1):
            print('下载失败')  # 如果是1 就是空文件 重新下载
        else:
            print('下载完成')

    print('将文件写入本地...')
    allonlne = []
    with open(fileName, 'wb') as f:
        for chunk in response.iter_content(1024):  # 防止文件过大，以1024为单位一段段写入
            f.write(chunk)
    print('写入完成')
    print('开始处理文件...')
    with open(fileName, 'r', encoding='utf-8') as f:
        reader = csv.reader(f, delimiter='\t')
        for li in reader:
            for row in li:
                # if row.contains("android"):
                if "android" in row:
                    continue
                allonlne.append(li[0])
    print('处理完成')
    return allonlne


def search(dirname, abNames):
    global scanTimes
    global problemAB
    filenames = os.listdir(dirname)
    for filename in filenames:
        if filename.startswith("."):
            continue
        if filename in blackPathList:
            continue
        fullFileName = os.path.join(dirname, filename)
        if os.path.isdir(fullFileName):
            search(fullFileName, abNames)
        else:
            if os.path.exists(fullFileName):
                ext = os.path.splitext(fullFileName)[-1]
                if ext == '.m' or ext == '.mm':
                    try:
                        with open(fullFileName, 'r', encoding='utf-8') as f:
                            # print("检索:", fullFileName)
                            scanTimes += 1
                            for line in f:
                                for abName in abNames:
                                    if abName in line:
                                        problemAB.add(abName)
                                        print("😄找到未下开关:{} 文件名:{}".format(abName, fullFileName))
                                        break
                    except PermissionError:
                        print("error")


# 写入文件
def write(fullname):
    with open('/Users/Sheldon/Downloads/ab.txt', 'w') as f:
        f.write(fullname+'\n')


def scan(abNames):
    print('开始扫描开关...')
    startTime = time.time()
    search(path, abNames)
    endTime = time.time()
    print('扫描结束')
    print('\n 总共检测了:{}个文件 扫描了:{}个开关 耗时:{}秒'.format(scanTimes, len(abNames), endTime-startTime))


if __name__ == '__main__':
    abNames = loadAB()
    # scan(abNames)
    print('开始扫描开关...')
    files = search_files_multithreaded(path, abNames)
    print('扫描结束')
